package edu.zqu.collegeField.project.service.impl;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.zqu.collegeField.common.utils.BeanMapper;
import edu.zqu.collegeField.framework.openfeign.api.GetCollegeNameApi;
import edu.zqu.collegeField.framework.openfeign.api.GetWorkerNameApi;
import edu.zqu.collegeField.project.po.Field;
import edu.zqu.collegeField.project.po.FieldWorkerExamine;
import edu.zqu.collegeField.project.mapper.FieldWorkerExamineMapper;
import edu.zqu.collegeField.project.service.FieldWorkerExamineService;
import edu.zqu.collegeField.project.vo.FieldVo;
import edu.zqu.collegeField.project.vo.FieldWorkerExamineVo;
import edu.zqu.common.core.text.Convert;
import edu.zqu.common.utils.StringUtils;
import net.bytebuddy.asm.Advice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigInteger;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 场地申请负责人审核结果 服务实现类
 * </p>
 *
 * @author 廖森
 * @since 2020-06-28
 */
@Service
public class FieldWorkerExamineServiceImpl extends ServiceImpl<FieldWorkerExamineMapper, FieldWorkerExamine> implements FieldWorkerExamineService {
    @Autowired
    private FieldWorkerExamineMapper fieldWorkerExamineMapper;
    @Autowired
    private GetCollegeNameApi getCollegeNameApi;
    @Autowired
    private GetWorkerNameApi getWorkerNameApi;

    @Override
    public List<FieldWorkerExamineVo> get(BigInteger collegeId,//获取场地申请负责人审核结果列表
                                          BigInteger fieldApplicationId,
                                          BigInteger workerId) {
        HashMap<String, String> map = new HashMap<>();
        map.put("collegeId", Convert.toStr(collegeId));
        map.put("fieldApplicationId", Convert.toStr(fieldApplicationId));
        map.put("workerId", Convert.toStr(workerId));
        List<FieldWorkerExamineVo> fieldWorkerExamineVoList = BeanMapper.mapList(fieldWorkerExamineMapper.get(map), FieldWorkerExamineVo.class);
        String collegeName = getCollegeNameApi.getCollegeName(collegeId);
        for (FieldWorkerExamineVo fieldWorkerExamineVo : fieldWorkerExamineVoList) {
            fieldWorkerExamineVo.setCollegeName(collegeName)
                    .setWorkerName(getWorkerNameApi.getWorkerName(fieldWorkerExamineVo.getWorkerId()));
        }
        return fieldWorkerExamineVoList;
    }

    @Override
    public FieldWorkerExamineVo getById(BigInteger id) {//获取场地申请负责人审核结果信息
        HashMap<String, String> map = new HashMap<>();
        map.put("id", Convert.toStr(id));
        FieldWorkerExamineVo fieldWorkerExamineVo = BeanMapper.map(fieldWorkerExamineMapper.getById(map), FieldWorkerExamineVo.class);
        String collegeName = getCollegeNameApi.getCollegeName(fieldWorkerExamineVo.getCollegeId());
        fieldWorkerExamineVo.setCollegeName(collegeName)
                .setWorkerName(getWorkerNameApi.getWorkerName(fieldWorkerExamineVo.getWorkerId()));
        return fieldWorkerExamineVo;
    }

    @Override
    public int add(FieldWorkerExamine fieldWorkerExamine) {//添加场地申请负责人审核结果
        fieldWorkerExamine.setId(null).setDeleted(null).setUpdateTime(new Timestamp(System.currentTimeMillis()));
        return fieldWorkerExamineMapper.insert(fieldWorkerExamine);
    }

    @Transactional(rollbackFor = MybatisPlusException.class)
    @Override
    public int update(FieldWorkerExamine fieldWorkerExamineUpdate) {//修改场地申请负责人审核结果
        FieldWorkerExamine fieldWorkerExamine = fieldWorkerExamineMapper.selectById(fieldWorkerExamineUpdate.getId());
        if (StringUtils.isNull(fieldWorkerExamine)) {
            throw new NullPointerException("找不到该场地");
        }
        BeanMapper.copyPropertiesNotNull(fieldWorkerExamineUpdate, fieldWorkerExamine);
        try {
            int insert = fieldWorkerExamineMapper.insert(fieldWorkerExamine.setLastVersion(fieldWorkerExamineUpdate.getId()));
            if (insert >= 1) {
                return delete(fieldWorkerExamineUpdate.getId(), fieldWorkerExamineUpdate.getByWho());
            }
        } catch (Throwable e) {
            throw new MybatisPlusException("更新失败");
        }
        return 0;
    }

    @Transactional(rollbackFor = MybatisPlusException.class)
    @Override
    public int delete(BigInteger id, BigInteger byWho) {//删除场地申请负责人审核结果
        try {
            UpdateWrapper<FieldWorkerExamine> fieldWorkerExamineUpdateWrapper = new UpdateWrapper<FieldWorkerExamine>()
                    .eq("id", Convert.toStr(id));
            int update = fieldWorkerExamineMapper.update(new FieldWorkerExamine().setByWho(byWho), fieldWorkerExamineUpdateWrapper);
            if (update >= 1) {
                return fieldWorkerExamineMapper.delete(fieldWorkerExamineUpdateWrapper);
            }
        } catch (Throwable e) {
            throw new MybatisPlusException("删除失败");
        }
        return 0;
    }
}
